<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/28
 * Time: 21:07
 */

namespace Home\Controller;


use Common\Utils\Constants;
use Home\Logic\AcceptLogLogic;
use Home\Logic\LotteryLogLogic;
use Home\Logic\PrizeLogic;

class LotteryController extends BaseController
{
    public function index(){
        $prizeList = $this->prizeList();
        $config = D('Config')->where(array('name'=>Constants::CONFIG_LOTTERY_RULES))->find();
        $lotteryGold = D('Config')->where(array('name' => Constants::CONFIG_LOTTERY_GOLD))->find();
        $acceptCode = D('Config')->where(array('name' => Constants::CONFIG_ACCEPT_QR_CODE))->find();
        $money = $lotteryGold['value'];
        if(!is_numeric($money) || $money<0){
            $money = 0;
        }
        $lottery_rules = html_entity_decode($config['value']);
        $this->assign('prizeList',$prizeList);
        $this->assign('qr_code',$acceptCode['value']);
        $this->assign('money',$money);
        $this->assign('lottery_rules',$lottery_rules);
        $this->display();
    }

    public function awardCode(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_ACCEPT_QR_CODE))->find();
        $this->assign('qr_code',$config['value']);
        $this->display();
    }

    /**
     * 跳转到领奖记录界面
     */
    public function acceptLog(){
        $this->display();
    }

    /**
     * 跳转到抽奖记录界面
     */
    public function lotteryLog(){
        $this->display();
    }

    /**
     * ajax 获取抽奖记录 $type == 0 抽奖记录 $type=1 领奖记录
     */
    public function ajaxLotteryLogList(){
        $pagesize = 20;
        $pagenumber = I('pagenum', '1');
        $type = I('type');
        $logic = new LotteryLogLogic();
        $user = $this->getUser();
        $map['user_id'] = $user['id'];
        if($type == 1){
            $map['type'] = array('neq',Constants::PRIZE_TYPE_FREE);
        }
        $data = $logic->getByConditionPage($map,$pagenumber,$pagesize);
        $this->assign('data', $data);
        if($type == 0){//抽奖记录
            $this->display('ajaxLotteryLogList');
        }else{//领奖记录
            $this->display('ajaxAcceptLogList');
        }
    }

    /**
     * 获取上架的奖品列表
     */
    private function prizeList(){
        $map['is_show'] = 1;
        $mop['is_delete'] = 0;
        $logic = new PrizeLogic();
        $list = $logic->getByConditionPage($map);
        return $list;
    }

    /**
     * ajax抽奖
     */
    public function lottery(){
        $user = $this->getUser();
        $logic = new LotteryLogLogic();
        $result = $logic->lottery($user['id']);
        $success = false;
        $message = '';
        switch ($result['status']){
            case 0:
                $message = '抽奖失败！';
                break;
            case 1:
                $success = true;
                $message = '恭喜您获得'.$result['prize']['title'].'！';
                break;
            case 2:
                $message = '对不起，暂时无法抽奖！';
                break;
            case 3:
                $message = '对不起，您的金币不足！';
                break;
        }
        $this->ajax($success,$message,-1,$result['prize']);
    }



//    public function saveLotteryLog(){
//        for($i=0;$i<20;$i++){
//            $log['create_time'] = time();
//            $log['type'] = $i % 4;
//            $log['money'] = get_rand_number(2);
//            $log['prize_id'] = 1;
//            $log['title'] = get_rand_code(20);
//            $log['status'] = $i % 2;
//            $log['user_id']  = 1;
//            D('LotteryLog')->add($log);
//        }
////        $log['']
//    }

}